package com.heima.user.service.impl;

import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.client.WemediaFeign;
import com.heima.client.ArticleFeign;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserReal;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.mapper.AuthPassMapper;
import com.heima.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserReal> implements ApUserRealnameService {

    List<ApUserReal> list;

    @Autowired
    private AuthPassMapper authPassMapper;

    @Autowired
    private WemediaFeign wmFeign;

    @Autowired
    private ArticleFeign articleFeign;

    /**
     * 查询结果
     * @param dto
     * @return
     */

    @Override
    public ResponseResult findApUserRealList(AuthDto dto) {
        //参数校验
        if (dto == null || dto.getPage() == null || dto.getSize() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        IPage page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<ApUserReal> wrapper = Wrappers.<ApUserReal>lambdaQuery();
        //根据用户状态查询
        if (dto.getStatus() != null) {
            wrapper.eq(ApUserReal::getStatus, dto.getStatus());
            this.page(page, wrapper);
        } else {
            this.page(page);
        }
        //封装结果返回
        return new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal(), page.getRecords());
    }

    /**
     * 审核通过
     * @param authDto
     * @return
     */
    @Override
    //@Transactional(rollbackFor = Exception.class)

    @GlobalTransactional(rollbackFor = Exception.class)
    public ResponseResult authPass(AuthDto authDto) {

        //校验参数
        if (authDto == null || authDto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //从用户认证列表中查询出要审核的用户
        ApUserReal apUserReal = this.getById(authDto.getId());
        //判断用户状态，只要当审核状态为1的时候才能审核
        Integer status = apUserReal.getStatus();
        if (status == null || status == 2 || status == 9) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "用户状态不可审核");
        }
        //如果可以审核就修改用户认证列表中的状态为9
        apUserReal.setStatus(9);
        this.updateById(apUserReal);
        //查询相关联的用户ap_user
        ApUser apUser = authPassMapper.selectById(apUserReal.getUserId());
        //以ap_user中的用户信息为基础，向自媒体库保存用户
        WmUser wmUser = new WmUser();
        wmUser.setApUserId(apUser.getId());
        wmUser.setApAuthorId(0);
        wmUser.setName(apUser.getName());
        wmUser.setPassword(apUser.getPassword());
        wmUser.setSalt(apUser.getSalt());
        wmUser.setImage(apUser.getImage());
        wmUser.setPhone(apUser.getPhone());
        ResponseResult<WmUser> responseResult = wmFeign.saveUser(wmUser);
        WmUser wmuser = responseResult.getData();
        //向文章库保存作者信息
        ApAuthor apAuthor = new ApAuthor();
        apAuthor.setName(apUser.getName());
        apAuthor.setType(2);
        apAuthor.setUserId(apUser.getId());
        apAuthor.setCreatedTime(new Date());
        apAuthor.setWmUserId(wmuser.getId());
        articleFeign.saveAuthor(apAuthor);
        //int a = 1/0;
        //返回响应结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 审核不通过
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDto authDto) {
        //校验参数
        if (authDto == null || authDto.getId() == null || StringUtils.isBlank(authDto.getMsg())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //从用户认证列表中查询出要审核的用户
        ApUserReal apUserReal = this.getById(authDto.getId());
        //修改用户认证表的状态
        //判断用户状态，只要当审核状态为1的时候才能审核
        Integer status = apUserReal.getStatus();
        if (status == null || status == 2 || status == 9) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "用户状态不可审核");
        }
        //如果不可以审核就修改用户认证列表中的状态为2
        apUserReal.setStatus(2);
        apUserReal.setReason(authDto.getMsg());
        this.updateById(apUserReal);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
